/*
*James Bienz(JB)
*Library CRUD ExercisesSet in the Database.
*
*GNU GPL v2
*/

package com.ist412.pocke.SQL;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ist412.pocke.DTO.ExerciseSetDTO;

public class ExerciseSet {

	//Table Name
	private static String TABLE_NAME= "ExerciseSet";
	
	//Set Indices
	private static final int ID_INDEX = 0;
	private static final int EXERCISE_ID_INDEX = 1;
	private static final int WORKOUT_ID_INDEX = 2;
	private static final int REPS_INDEX = 3;
	private static final int WEIGHT_INDEX = 4;
	private static final int SEQUENCE_NUM_INDEX = 5;
	private static final int TIME_INDEX = 6;
	private static final int NOTE_INDEX = 7;
	
	//Set Keys
	private static final String EXERCISE_ID_KEY = "EXERCISE_ID";
	private static final String WORKOUT_ID_KEY = "WORKOUT_ID";
	private static final String REPS_KEY = "reps";
	private static final String WEIGHT_KEY = "weight";
	private static final String SEQUENCE_NUM_KEY = "sequenceNum";
	private static final String TIME_KEY = "time";
	private static final String NOTE_KEY = "note";
	
	public static ArrayList<ExerciseSetDTO> getExerciseSets(Context context){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getReadableDatabase();
		ArrayList<ExerciseSetDTO> exerciseSets = new ArrayList<ExerciseSetDTO>();
		String query = "SELECT * FROM ExerciseSet";
		Cursor cur = db.rawQuery(query, null);
		while(cur.moveToNext()){
			ExerciseSetDTO exerciseSet = new ExerciseSetDTO();
			exerciseSet.setId(cur.getInt(ID_INDEX));
			exerciseSet.setExerciseId(cur.getInt(EXERCISE_ID_INDEX));
			exerciseSet.setWorkoutId(cur.getInt(WORKOUT_ID_INDEX));
			exerciseSet.setReps(cur.getInt(REPS_INDEX));
			exerciseSet.setWeight(cur.getInt(WEIGHT_INDEX));
			exerciseSet.setSequenceNum(cur.getInt(SEQUENCE_NUM_INDEX));
			exerciseSet.setTime(cur.getInt(TIME_INDEX));
			exerciseSet.setNote(cur.getString(NOTE_INDEX));
			exerciseSets.add(exerciseSet);
		}
		return exerciseSets;
	}
	
	public static ExerciseSetDTO getExerciseSet(Context context, int id){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getReadableDatabase();
		ExerciseSetDTO exerciseSet = new ExerciseSetDTO();
		String query = "SELECT * FROM ExerciseSet WHERE id=?";
		Cursor cur = db.rawQuery(query, new String[] {String.valueOf(id)});
		if(cur.moveToFirst()){
			exerciseSet.setId(cur.getInt(ID_INDEX));
			exerciseSet.setExerciseId(cur.getInt(EXERCISE_ID_INDEX));
			exerciseSet.setWorkoutId(cur.getInt(WORKOUT_ID_INDEX));
			exerciseSet.setReps(cur.getInt(REPS_INDEX));
			exerciseSet.setWeight(cur.getInt(WEIGHT_INDEX));
			exerciseSet.setSequenceNum(cur.getInt(SEQUENCE_NUM_INDEX));
			exerciseSet.setTime(cur.getInt(TIME_INDEX));
			exerciseSet.setNote(cur.getString(NOTE_INDEX));
		}
		return exerciseSet;
	}
	
	public static ArrayList<ExerciseSetDTO> getExerciseSet(Context context, int workoutID, int exerciseID){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getReadableDatabase();
		ArrayList<ExerciseSetDTO> exerciseSets = new ArrayList<ExerciseSetDTO>();
		String query = "SELECT * FROM ExerciseSet WHERE EXERCISE_ID=? AND WORKOUT_ID=? ";
		Cursor cur = db.rawQuery(query, new String[] {String.valueOf(exerciseID), String.valueOf(workoutID)});
		while(cur.moveToNext()){
			ExerciseSetDTO exerciseSet = new ExerciseSetDTO();
			exerciseSet.setId(cur.getInt(ID_INDEX));
			exerciseSet.setExerciseId(cur.getInt(EXERCISE_ID_INDEX));
			exerciseSet.setWorkoutId(cur.getInt(WORKOUT_ID_INDEX));
			exerciseSet.setReps(cur.getInt(REPS_INDEX));
			exerciseSet.setWeight(cur.getInt(WEIGHT_INDEX));
			exerciseSet.setSequenceNum(cur.getInt(SEQUENCE_NUM_INDEX));
			exerciseSet.setTime(cur.getInt(TIME_INDEX));
			exerciseSet.setNote(cur.getString(NOTE_INDEX));
			exerciseSets.add(exerciseSet);
		}
		return exerciseSets;
	}
	
	public static void createExerciseSet(Context context, ExerciseSetDTO exerciseSet){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(EXERCISE_ID_KEY, exerciseSet.getExerciseId());
		values.put(WORKOUT_ID_KEY, exerciseSet.getWorkoutId());
		values.put(REPS_KEY, exerciseSet.getReps());
		values.put(WEIGHT_KEY, exerciseSet.getWeight());
		values.put(SEQUENCE_NUM_KEY, exerciseSet.getSequenceNum());
		values.put(TIME_KEY, exerciseSet.getTime());
		values.put(NOTE_KEY, exerciseSet.getNote());
		db.insert(TABLE_NAME, null, values);
	}
	
	public static void updateExerciseSet(Context context, ExerciseSetDTO exerciseSet){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(EXERCISE_ID_KEY, exerciseSet.getExerciseId());
		values.put(WORKOUT_ID_KEY, exerciseSet.getWorkoutId());
		values.put(REPS_KEY, exerciseSet.getReps());
		values.put(WEIGHT_KEY, exerciseSet.getWeight());
		values.put(SEQUENCE_NUM_KEY, exerciseSet.getSequenceNum());
		values.put(TIME_KEY, exerciseSet.getTime());
		values.put(NOTE_KEY, exerciseSet.getNote());
		String filter = "id=" + exerciseSet.getId();
		db.update(TABLE_NAME, values, filter, null);
	}
	
	public static void deleteExerciseSet(Context context, int id){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getWritableDatabase();
		String whereClause = "id=" + id;
		db.delete(TABLE_NAME, whereClause, null);
	}
}
